package com.hz.web.controller;

import com.hz.web.entity.*;
import com.hz.web.service.*;
import com.hz.web.util.ParmsEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Wangh on 2018/4/28.
 */
@Controller
@RequestMapping("companyMange")
public class CompanyManageController {

    @Resource
    private ICompanyManageService iCompanyManageService;
    @Resource
    private ICompanyBaseService companyBaseService;
    @Resource
    private ICompanyEmployeeStatusService companyEmployeeStatusService;
    @Resource
    private ICompanyLevelInfoService companyLevelInfoService;
    @Resource
    private ICertificatoryCampanyService certificatoryCampanyService;

    @Resource
    private IemployeeService employeeService;

    @Resource
    private ICertificatoryPerService certificatoryPerService;

    /**
     * 查询
     *
     * @return
     */
    @RequestMapping("queryCompanyList")
    @ResponseBody
    public Map<String, Object> queryCompanyList(ParmsEntity entity) {
        Map<String, Object> resultMap = new HashMap<String, Object>();//结果map
        Map<String, Object> parmsMap = new HashMap<String, Object>();//参数map
        int page = entity.getPage();
        int rows = entity.getRows();
        if (page != 0 && rows != 0) {
            int index = (page - 1) * rows;
            int end = rows;
            parmsMap.put("index", index);
            parmsMap.put("end", end);
        }

        List<CompanyBaseInfo> list = new ArrayList<CompanyBaseInfo>();
        String showName = entity.getShowName();
        String name = entity.getUserName();
        if (showName != null && !"".equals(showName.trim())) {
            parmsMap.put("adminShowName", "%" + showName.trim() + "%");
        }
        if (name != null && !"".equals(name)) {
            parmsMap.put("adminName", "%" + name.trim() + "%");
        }
        int count = 0;
        list = iCompanyManageService.queryCompanyList(parmsMap);
        count = iCompanyManageService.queryCompanyCount(parmsMap);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        resultMap.put("rows", list);
        resultMap.put("total", count);
        return resultMap;
    }

    /**
     * 根据企业id查询企业基本信息
     *
     * @return
     */
    @RequestMapping("baseInfoByUserId")
    @ResponseBody
    public Map<String, Object> baseInfoByUserId(ParmsEntity entity, Integer userId) {
        Map<String, Object> resultMap = new HashMap<String, Object>();//结果map
        Map<String, Object> parmsMap = new HashMap<String, Object>();//参数map

        CompanyBaseInfo companyBaseInfo = companyBaseService.selectByUserId(userId);
        CompanyEmployeeStatus companyEmployeeStatus = companyEmployeeStatusService.selectByCompanyId(companyBaseInfo.getId());
        CompanyLevelInfo companyLevelInfo = companyLevelInfoService.selectByCompanyId(companyBaseInfo.getId());

        if (companyEmployeeStatus == null) {
            companyEmployeeStatus = new CompanyEmployeeStatus();
            companyEmployeeStatus.setCompanyId(companyBaseInfo.getId());
        }
        if (companyLevelInfo == null) {
            companyLevelInfo = new CompanyLevelInfo();
            companyLevelInfo.setCompanyId(companyBaseInfo.getId());
        }

        companyBaseInfo.setCompanyEmployeeStatus(companyEmployeeStatus);
        companyBaseInfo.setCompanyLevelInfo(companyLevelInfo);

        resultMap.put("entity", companyBaseInfo);
        return resultMap;
    }


    /**
     * 企业授证
     *
     * @param
     * @return
     */
    @RequestMapping("addCertificatoryCampany")
    @ResponseBody
    public Map<String, Object> addCertificatoryCampany(CertificatoryComp entity) {
        Map<String, Object> resultMap = new HashMap();
        int row = -1;
        entity.setCertificateLevel(entity.getCertificateLevel());
        entity.setCertificateNum(entity.getCertificateNum());
        entity.setCertificateValidity(entity.getCertificateValidity());
        entity.setCertificateTime(new Date());
        entity.setCertificateType(entity.getCertificateType());
        entity.setCompanyId(entity.getCompanyId());
        entity.setCompanyName(entity.getCompanyName());

        try {
            row = this.certificatoryCampanyService.insertertificatory(entity);
        } catch (Exception var7) {
            var7.printStackTrace();
            resultMap.put("row", Integer.valueOf(row));
            resultMap.put("msg", "添加异常");
            return resultMap;
        }

        resultMap.put("row", Integer.valueOf(row));
        resultMap.put("msg", "企业授证成功！");
        return resultMap;

    }

    /**
     * 个人授证
     *
     * @param
     * @return
     */
    @RequestMapping("addCertificatoryPersonal")
    @ResponseBody
    public Map<String, Object> addCertificatoryPersonal(CertificatoryPer entity) {
        Map<String, Object> resultMap = new HashMap();
        int row = -1;
        int updated = -1;

        entity.setCertificateTime(new Date());
        try {
            row = this.certificatoryPerService.insertCertificatory(entity);
            if (-1 != row) {
                updated = certificatoryPerService.updateCertificatory(entity.getBelCompany());
            }
        } catch (Exception var7) {
            var7.printStackTrace();
            resultMap.put("row", Integer.valueOf(row));
            resultMap.put("msg", "添加异常");
            return resultMap;
        }

        resultMap.put("row", Integer.valueOf(row));
        resultMap.put("msg", "个人授证成功！");
        return resultMap;

    }


    /**
     * 个人证书查询
     *
     * @param
     * @return
     */
    @RequestMapping("queryCertificatoryPersonal")
    @ResponseBody
    public Map<String, Object> queryCertificatoryPersonal(ParmsEntity entity, String userId) {
        Map<String, Object> resultMap = new HashMap();
        Map<String, Object> parmsMap = new HashMap<String, Object>();//参数map
        int page = entity.getPage();
        int rows = entity.getRows();
        if (page != 0 && rows != 0) {
            int index = (page - 1) * rows;
            int end = rows;
            parmsMap.put("index", index);
            parmsMap.put("userName", entity.getUserName());
            parmsMap.put("end", end);
            parmsMap.put("userId", userId);
        }
        List<CertificatoryPer> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        int count = 0;
        try {
            list = this.certificatoryPerService.selectByUserId(parmsMap);
            if (list.size() > 0) {
                for (CertificatoryPer certificatoryPer : list) {
                    if (certificatoryPer.getCertificateTime() != null) {
                        certificatoryPer.setCertificateTimeStr(sdf.format(certificatoryPer.getCertificateTime()));
                    }
                    if (certificatoryPer.getValidityTime() != null) {
                        certificatoryPer.setValidityTimeStr(sdf.format(certificatoryPer.getCertificateTime()));
                    }
                }
            }
            count = certificatoryPerService.selectByUserIdCount(parmsMap);
        } catch (Exception var7) {
            var7.printStackTrace();

        }

        resultMap.put("rows", list);
        resultMap.put("total", count);
        return resultMap;

    }

    /**
     * 企业证书查询
     *
     * @param
     * @return
     */
    @RequestMapping("queryCertificatoryCompany")
    @ResponseBody
    public Map<String, Object> queryCertificatoryCompany(ParmsEntity entity, String companyId) {
        Map<String, Object> resultMap = new HashMap();
        Map<String, Object> parmsMap = new HashMap<String, Object>();//参数map
        int page = entity.getPage();
        int rows = entity.getRows();
        if (page != 0 && rows != 0) {
            int index = (page - 1) * rows;
            int end = rows;
            parmsMap.put("index", index);
            parmsMap.put("userName", entity.getUserName());
            parmsMap.put("end", end);
            parmsMap.put("companyId", companyId);
        }
        List<CertificatoryComp> list = new ArrayList<>();
        int count = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            list = this.certificatoryCampanyService.selectByCompanyId(parmsMap);
            if (list.size() > 0) {
                for (CertificatoryComp certificatoryComp : list) {
                    if (certificatoryComp.getCertificateTime() != null) {
                        certificatoryComp.setCertificateTimeStr(sdf.format(certificatoryComp.getCertificateTime()));
                    }
                    if (certificatoryComp.getCertificateValidity() != null) {
                        certificatoryComp.setCertificateValidityStr(sdf.format(certificatoryComp.getCertificateValidity()));
                    }
                }
            }
            count = certificatoryCampanyService.selectByCompanyIdCount(parmsMap);
        } catch (Exception var7) {
            var7.printStackTrace();
            return resultMap;
        }

        resultMap.put("rows", list);
        resultMap.put("total", count);
        return resultMap;

    }

//    /**
//     * 跳转到授证页面
//     *
//     * @param session
//     * @param model
//     * @return
//     */
//    @RequestMapping("toCertificatoryCompany")
//    public String toCertificatory(HttpSession session, Model model, String userId) {
//        Map<String, Object> resultMap = new HashMap();
//        int row = -1;
//        List<CertificatoryComp> cc = new ArrayList<>();
//
//        try {
//            cc = this.certificatoryCampanyService.selectByCompanyId(userId);
//        } catch (Exception var7) {
//            var7.printStackTrace();
//
//
//        }
//        model.addAttribute("companyId", userId);
//        return "certificatory_company";
//    }


    /**
     * 删除企业证书信息
     *
     */
    @RequestMapping("delCertificatory")
    @ResponseBody
    public Map<String, Object> delCertificatory(String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();


        int delrow = -1;
        try {
            CertificatoryComp certificatoryComp = new CertificatoryComp();
            certificatoryComp.setCertificateId(Integer.parseInt(id));
            delrow = certificatoryCampanyService.deleteByPrimaryKey(certificatoryComp);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("msg", "删除异常！");
            resultMap.put("row", delrow);
        }
        if (delrow >= 0) {
            resultMap.put("msg", "删除成功！");
        } else {
            resultMap.put("msg", "删除失败！");
        }

        resultMap.put("row", delrow);
        return resultMap;
    }

}
